using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace PinTuGame
{
    public partial class LeaderboardForm : Form
    {
        private Leaderboard leaderboard;
        private ComboBox difficultyFilterComboBox;
        private DataGridView recordsDataGridView;
        private Button clearButton;

        public LeaderboardForm(Leaderboard leaderboard)
        {
            this.leaderboard = leaderboard;
            InitializeComponents();
            LoadRecords();
        }

        private void InitializeComponents()
        {
            this.Text = "游戏排行榜";
            this.Size = new Size(800, 600);
            this.StartPosition = FormStartPosition.CenterParent;
            this.MinimizeBox = false;

            // 创建主布局
            TableLayoutPanel mainLayout = new TableLayoutPanel
            {
                Dock = DockStyle.Fill,
                ColumnCount = 1,
                RowCount = 3,
                RowStyles = 
                {
                    new RowStyle(SizeType.Absolute, 50),
                    new RowStyle(SizeType.Percent, 100),
                    new RowStyle(SizeType.Absolute, 40)
                }
            };

            // 标题和筛选面板
            Panel filterPanel = new Panel
            {
                Dock = DockStyle.Fill,
                BackColor = Color.LightBlue
            };

            Label titleLabel = new Label
            {
                Text = "游戏排行榜",
                Font = new Font("微软雅黑", 16, FontStyle.Bold),
                TextAlign = ContentAlignment.MiddleCenter,
                Dock = DockStyle.Top,
                Height = 30
            };
            filterPanel.Controls.Add(titleLabel);

            Label filterLabel = new Label
            {
                Text = "难度筛选:",
                Location = new Point(20, 35),
                AutoSize = true,
                Font = new Font("微软雅黑", 10)
            };
            filterPanel.Controls.Add(filterLabel);

            difficultyFilterComboBox = new ComboBox
            {
                Location = new Point(100, 32),
                Width = 120,
                DropDownStyle = ComboBoxStyle.DropDownList,
                Font = new Font("微软雅黑", 9)
            };
            difficultyFilterComboBox.Items.AddRange(new object[] { "全部", "简单(3×3)", "中等(4×4)", "困难(5×5)" });
            difficultyFilterComboBox.SelectedIndex = 0;
            difficultyFilterComboBox.SelectedIndexChanged += DifficultyFilterComboBox_SelectedIndexChanged;
            filterPanel.Controls.Add(difficultyFilterComboBox);

            // 数据网格
            recordsDataGridView = new DataGridView
            {
                Dock = DockStyle.Fill,
                AllowUserToAddRows = false,
                AllowUserToDeleteRows = false,
                ReadOnly = true,
                SelectionMode = DataGridViewSelectionMode.FullRowSelect,
                MultiSelect = false,
                AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill,
                Font = new Font("微软雅黑", 9)
            };

            // 设置列
            recordsDataGridView.Columns.AddRange(new DataGridViewColumn[]
            {
                new DataGridViewTextBoxColumn { HeaderText = "排名", Name = "Rank", Width = 50 },
                new DataGridViewTextBoxColumn { HeaderText = "玩家", Name = "PlayerName", Width = 100 },
                new DataGridViewTextBoxColumn { HeaderText = "难度", Name = "Difficulty", Width = 80 },
                new DataGridViewTextBoxColumn { HeaderText = "用时", Name = "PlayTime", Width = 80 },
                new DataGridViewTextBoxColumn { HeaderText = "步数", Name = "MoveCount", Width = 60 },
                new DataGridViewTextBoxColumn { HeaderText = "完成时间", Name = "CompletionDate", Width = 120 },
                new DataGridViewTextBoxColumn { HeaderText = "图片", Name = "ImageName", Width = 100 }
            });

            // 按钮面板
            Panel buttonPanel = new Panel
            {
                Dock = DockStyle.Fill
            };

            clearButton = new Button
            {
                Text = "清空排行榜",
                Size = new Size(100, 30),
                Location = new Point(20, 5),
                Font = new Font("微软雅黑", 10)
            };
            clearButton.Click += ClearButton_Click;
            buttonPanel.Controls.Add(clearButton);

            Button closeButton = new Button
            {
                Text = "关闭",
                Size = new Size(80, 30),
                Location = new Point(700, 5),
                Font = new Font("微软雅黑", 10)
            };
            closeButton.Click += (s, e) => this.Close();
            buttonPanel.Controls.Add(closeButton);

            mainLayout.Controls.Add(filterPanel, 0, 0);
            mainLayout.Controls.Add(recordsDataGridView, 0, 1);
            mainLayout.Controls.Add(buttonPanel, 0, 2);

            this.Controls.Add(mainLayout);
        }

        private void LoadRecords()
        {
            recordsDataGridView.Rows.Clear();
            string selectedDifficulty = difficultyFilterComboBox.SelectedItem.ToString();

            var records = selectedDifficulty == "全部" 
                ? leaderboard.GetAllRecords() 
                : leaderboard.GetTopRecords(selectedDifficulty, 50);

            int rank = 1;
            foreach (var record in records)
            {
                recordsDataGridView.Rows.Add(
                    rank++,
                    record.PlayerName,
                    record.Difficulty,
                    $"{record.PlayTime:mm\\:ss}",
                    record.MoveCount,
                    record.CompletionDate.ToString("yyyy-MM-dd HH:mm"),
                    record.ImageName
                );
            }
        }

        private void DifficultyFilterComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadRecords();
        }

        private void ClearButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要清空所有排行榜记录吗？此操作不可恢复！", "确认清空", 
                MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                leaderboard.ClearRecords();
                LoadRecords();
            }
        }
    }
} 